package com.wman.drawing.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.wman.drawing.R;
import com.wman.sheep.common.utils.DensityUtil;

import androidx.annotation.Nullable;

/**
 * @ProjectName: wman
 * @Package: com.wman.drawing.view
 * @ClassName: SportsView
 * @Description: java类作用描述
 * @Author: wman
 * @CreateDate: 2019/11/6 9:42
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/11/6 9:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ImageTextView extends View {
    private final static int RING_WIDTH = DensityUtil.dip2px(20);
    private final static int IMAGE_WIDTH = DensityUtil.dip2px(150);
    private final static int CIRCLE_COLOR = Color.parseColor("#90A4AE");
    private final static int HIGHTLIGHT_COLOR = Color.parseColor("#FF4081");
    private final static String TEXT = "静态变量是一个引用。这种情况比较特殊，主要问题是由于静态变量是一个对象的引用，那么必须初始化这个对象之后才能将引用指向它。因此如果要把一个引用定义成static的，就必须在定义的时候就对其对象进行初始化。静态变量是一个引用。这种情况比较特殊，主要问题是由于静态变量是一个对象的引用，那么必须初始化这个对象之后才能将引用指向它。因此如果要把一个引用定义成static的，就必须在定义的时候就对其对象进行初始化。静态变量是一个引用。这种情况比较特殊，主要问题是由于静态变量是一个对象的引用，那么必须初始化这个对象之后才能将引用指向它。因此如果要把一个引用定义成static的，就必须在定义的时候就对其对象进行初始化。静态变量是一个引用。这种情况比较特殊，主要问题是由于静态变量是一个对象的引用，那么必须初始化这个对象之后才能将引用指向它。因此如果要把一个引用定义成static的，就必须在定义的时候就对其对象进行初始化。";

    private float[] cutWidth = new float[1];
    private StaticLayout mStaticLayout;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

    private Bitmap mBitmap;

    public ImageTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    {
        mPaint.setTextSize(DensityUtil.dip2px(12));
        mTextPaint.setTextSize(DensityUtil.dip2px(12));
        mStaticLayout = new StaticLayout(TEXT, mTextPaint, getWidth(), Layout.Alignment.ALIGN_NORMAL, 1, 0, false);

        mBitmap = getAvatar(IMAGE_WIDTH);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mStaticLayout.draw(canvas);
        canvas.drawBitmap(mBitmap, getWidth() - mBitmap.getWidth(), 100, mPaint);
        int index = mPaint.breakText(TEXT, true, getWidth(), cutWidth);
        canvas.drawText(TEXT, 0, index, 0, 50, mPaint);
        int oldIndex = index;
        index = mPaint.breakText(TEXT, index, TEXT.length(), true, getWidth(), cutWidth);
        canvas.drawText(TEXT, oldIndex, oldIndex + index, 0, 50 + mPaint.getFontSpacing(), mPaint);
        oldIndex = index;
        index = mPaint.breakText(TEXT, index, TEXT.length(), true, getWidth() - mBitmap.getWidth(), cutWidth);
        canvas.drawText(TEXT, oldIndex, oldIndex + index, 0, 50 + mPaint.getFontSpacing() + mPaint.getFontSpacing(), mPaint);
    }

    private Bitmap getAvatar(int width) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.mipmap.icon_avatar, options);
        options.inJustDecodeBounds = false;
        options.inDensity = options.outWidth;
        options.inTargetDensity = width;
        return BitmapFactory.decodeResource(getResources(), R.mipmap.icon_avatar, options);
    }
}
